package com.sltx.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;


@Component
public class DownloadUtil {


    //文件保存路径
    @Value("${file-save-path}")
    private String fileSavePath;

    //项目启动根目录
    @Value("${server.servlet.context-path}")
    private String locationPath;


    public String  getDownload(PageData pd,  String[] parameterValues, HttpServletRequest request)throws Exception {

        List<File> fileList = new ArrayList<>();
        // 获取营业执照并把营业执照路径放入fileList
        if (pd.get("yyzzimg")!= null) {
            String yyzzimg = pd.get("yyzzimg").toString();
            System.out.println(yyzzimg);
            String[] yy = yyzzimg.split("image/");
            fileList.add(new File(fileSavePath + "image/" + yy[1]));
        }
        // 获取认定表列表并把认定表路径放入fileList
        if (parameterValues != null) {
            if (parameterValues.length > 0) {
                for (int i = 0; i < parameterValues.length; i++) {
                    String rdbimg = parameterValues[i].toString();
                    System.out.println(rdbimg);
                    String[] rdb = rdbimg.split("image/");
                    fileList.add(new File(fileSavePath + "image/" + rdb[1]));
                }
            }
        }
        // 获取文档并把文档路径放入fileList
        if (pd.get("doc") != null) {
            String doc = pd.get("doc").toString();
            System.out.println(doc);
            String[] word = doc.split("word/");
            fileList.add(new File(fileSavePath+ "word/" + word[1]));
        }

        String entname = pd.get("entname").toString();
        String filename = entname+".zip";
        FileOutputStream fos2 = new FileOutputStream(new File(fileSavePath +  filename ));
        try {
            ZipUtils.toZip(fileList, fos2);
            //windows
            //String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/sjs_product/aaa/" + filename;

            //linux
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + locationPath + fileSavePath + filename;
            return url;
        }catch (Exception e){
            e.printStackTrace();
            return "下载失败";
        }
    }

    public String getDownUrl(List list, HttpServletRequest request) throws Exception{
        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            // 切割下载路径，得到文件名称
            String[] split = list.get(i).toString().split("sjs_qysj/");
            // fileSavePath  + split[1] 得到是文件的绝对路径
            fileList.add(new File(fileSavePath  + split[1]));

        }

        String entname = "全部企业";
        String filename = entname+".zip";
        FileOutputStream fos2 = new FileOutputStream(new File(fileSavePath + "zip/" + filename ));
        try {
            ZipUtils.toZip(fileList, fos2);
            //windows
//            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/sjs_product/aaa/zip/" + filename;

            //linux
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + locationPath + fileSavePath + "zip/" + filename;
            return url;
        }catch (Exception e){
            e.printStackTrace();
            return "下载失败";
        }
    }






}
